$(function () {

    //点击按钮提示框出现
    $("#J_LinkBasket").click(function () {
        // console.log('aaa')
        $("#POPBOX").css("display", "block");
    })

    //点击X提示框消失
    $(".modPup_close").click(function () {
        $("#POPBOX").css("display", "none");
    })


    //点击按钮数值增减
    $(".amount-decrease").click(function () {
        changeGoodsCount($(this).index(".amount-decrease"), "reduce")
    });
    $(".amount-increase").click(function () {
        changeGoodsCount($(this).index(".amount-increase"), "add")
    });

    function changeGoodsCount(index, type) {
        var $goods_count = $("#J_shopcart-tx-num").eq(index);
        var count = $goods_count.val();
        // 如果是减，那么我们需要判定count为1的情况; 
        if (count <= 1 && type === "reduce") {
            if (confirm("是否要删除当前商品!")) {
                removeGoods(index);
            }
            return false;
        }
        // 分成reduce减，还有add加; 
        switch (type) {
            case "add":
                count++; break;
            case "reduce":
                count--; break;
        }

        $goods_count.val(count);

    }

    
    $("#J_LinkBasket").click(function () {
        $.ajax({
            url : "./date.json"
        }).done(function(res){
            // console.log(res
            res.data.filter(function(item){
                $("#J_LinkBasket").attr("data-id" , item.id);
                addCart(item.id);
            })
        })
       
    });
    var shop_list = (function () {
        if (localStorage.getItem("cart")) {
            return JSON.parse(localStorage.getItem("cart"));
        } else {
            return {};
        }
    })();
    function addCart(id) {

        if (id in shop_list) {
            shop_list[id]++
        } else {
            shop_list[id] = 1;
        }
        localStorage.setItem("cart", JSON.stringify(shop_list));
    }
})
